package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhwl.common.dto.cent.sdata.RegionDTO;
import com.xhwl.common.pojo.cent.sdata.Region;
import com.xhwl.common.query.RegionQuery;
import com.xhwl.data.dao.IRegionDao;
import com.xhwl.data.service.IRegionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 11:34 2021/1/17
 */
@Service
public class RegionServiceImpl implements IRegionService {


    @Autowired
    private IRegionDao regionDao;

    @Override
    public List<Region> findByIds(Collection<Integer> ids) {
        if (null == ids || ids.isEmpty()) {
            return Collections.emptyList();
        }
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        return regionDao.selectList(queryWrapper);

    }

    @Override
    @Cacheable(value = "RegionServiceImpl.tree", unless = "#query.keywords != null")
    public List<RegionDTO> tree(RegionQuery query) {
        if (null == query) {
            return treeMaker(all(null));
        } else {
            QueryWrapper<Region> queryWrapper = new QueryWrapper();
            queryHandler(query, queryWrapper);
            return treeMaker(regionDao.selectList(queryWrapper));
        }

    }

    private List<RegionDTO> treeMaker(List<Region> all) {

        List<RegionDTO> allDTO = new ArrayList<>();
        all.stream().forEach(a -> {
            RegionDTO regionDTO = new RegionDTO();
            BeanUtils.copyProperties(a, regionDTO);
            allDTO.add(regionDTO);
        });
        List<RegionDTO> top = allDTO.stream().filter(t -> 1 == t.getLevel()).collect(Collectors.toList());
        top.stream().forEach(t -> t.setChildren(getChild(allDTO, t)));
        return top;
    }

    private List<RegionDTO> getChild(List<RegionDTO> regions, RegionDTO parent) {

        List<RegionDTO> children = regions.stream().filter(r -> parent.getId() == r.getPid().intValue()).collect(Collectors.toList());
        if (children.isEmpty())
            return null;
        parent.setChildren(children);
        children.parallelStream().forEach(c -> c.setChildren(getChild(regions, c)));
        return children;
    }

    @Override
    @Cacheable(value = "RegionServiceImpl.all")
    public List<Region> all(Integer level) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        if (null != level) {
            queryWrapper.eq("level", level);
        }
        return regionDao.selectList(queryWrapper);
    }

    @Override
    @Cacheable(value = "RegionServiceImpl.list", unless = "#query.keywords != null")
    public List<Region> list(RegionQuery query) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        if (null != query) {
            queryHandler(query, queryWrapper);
            return regionDao.selectList(queryWrapper);
        }
        return all(null);
    }

    private void queryHandler(RegionQuery query, QueryWrapper<Region> queryWrapper) {

        if (null != query.levels && !query.levels.isEmpty()) {
            queryWrapper.in("level", query.levels);
        }
        if (StringUtils.isNotEmpty(query.keywords)) {
            queryWrapper.like("name", query.keywords);
        }
        if (StringUtils.isNotEmpty(query.name)) {
            queryWrapper.eq("name", query.name);
        }
        if (null != query.parentId) {
            queryWrapper.eq("pid", query.parentId);
        }
    }

    /**
     * 获取省市区名称
     * @param provincesId
     * @param cityId
     * @param districtId
     * @return
     */
    public String getRegion(Integer provincesId, Integer cityId, Integer districtId) {
        List<Integer> integers = Arrays.asList(provincesId, cityId, districtId);
        List<Region> regions = findByIds(integers);
        if (regions.isEmpty()) {
            return "";
        }
        Map<Integer, String> reMap = regions.stream().collect(Collectors.toMap(Region::getId, Region::getName));
        StringBuilder sbd = new StringBuilder();
        if (null != provincesId && com.xhwl.common.utils.StringUtils.isNotEmpty(reMap.get(provincesId))) {
            sbd.append(null == reMap.get(provincesId)? "" : reMap.get(provincesId));
        }
        if (null != cityId && com.xhwl.common.utils.StringUtils.isNotEmpty(reMap.get(cityId))) {
            sbd.append(null == reMap.get(cityId)? "" : reMap.get(cityId));
        }
        if (null != districtId && com.xhwl.common.utils.StringUtils.isNotEmpty(reMap.get(districtId))) {
            sbd.append(null == reMap.get(districtId)? "" : reMap.get(districtId));
        }
        return sbd.toString();
    }

}
